﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///
///</summary>
public class PlayerComponent : MonoBehaviour
{
    Animator animator;

    float distance;
    bool moveing = false;
    Vector2 EnemyPos;
    // Start is called before the first frame update
    private static PlayerComponent instance;

    public static PlayerComponent Instance
    {
        get
        {
            if (instance == null)
            {
                instance = GameObject.Find("Canvas/GameView/player").GetComponent<PlayerComponent>();
            }
            return instance;
        }
    }


    void Start()
    {
        animator = transform.GetComponent<Animator>();
    }

  

    // Update is called once per frame
    void Update()
    {
        if (Input.anyKeyDown)
        {
            animator.SetBool("IsAttack", true);            
        }
        else
        {
            animator.SetBool("IsAttack", false);
        }

        if (moveing)
        {
            transform.position = Vector3.Lerp(transform.position, EnemyPos, Time.deltaTime*2);
            animator.SetBool("IsRight", false);
            animator.SetBool("IsLeft", false);
            if (Math.Abs(transform.position.x - EnemyPos.x)<=10)
            {
                moveing = false;
            }
        }
    }

    public void MoveingToEnemyPosX(Vector3 pos)
    {
        EnemyPos = new Vector3(pos.x, transform.position.y);
        if (pos.x >= transform.position.x)
        {
            animator.SetBool("IsRight", true);
        }
        else
        {
            animator.SetBool("IsLeft", true);
        }
        moveing = true;
    }

}